package com.nio.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Method;

@Component
@Aspect
public class LogAspect {
    @Pointcut("@annotation(com.nio.log.Mylog)")
    public void mylog(){

    }

    // 通知
    @Before("mylog()")
    public void beforeAop(){
        System.out.println("前置通知");
    }

    @After("mylog()")
    public void afterAop(){
        System.out.println("后置通知");
    }

    @Around("mylog()")
    public void aroundAop(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("环绕通知");
        proceedingJoinPoint.proceed();
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature(); // 获取请求的方法void com.nio.service.impl.AopDemoImpl.devAop()
        System.out.println(signature);
        Method method = signature.getMethod();  // 方法的属性public void com.nio.service.impl.AopDemoImpl.devAop()
        System.out.println(method);
        // 获取@Log注解的value值
        Class<?> aClass = proceedingJoinPoint.getTarget().getClass();
        Mylog annotation = aClass.getAnnotation(Mylog.class);
        if(!ObjectUtils.isEmpty(annotation)){
            String value = annotation.value();
            System.out.println(value);
        }
        // 参数
        Object[] args = proceedingJoinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i].toString());
        }
    }
}
